package com.lmk.ms.common.net.udp.client.service.impl;

import com.lmk.ms.common.net.udp.client.service.UdpClientService;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.net.DatagramSocket;
import java.net.InetAddress;

/**
 * UDP客户端业务服务适配器
 * @author laomake@hotmail.com
 * @version 1.0
 * @date 2023/4/25
 */
@Slf4j
public abstract class UdpClientServiceAdapter implements UdpClientService {
    @Override
    public void handel(DatagramSocket socket, InetAddress serverAddress, int serverPort) {
        if(socket == null) {
            log.error("业务处理失败，socket不可为空！");
            return;
        }

        log.debug("业务开始");
        try{
            run(socket, serverAddress, serverPort); // 调用子类实现的方法
        } catch (IOException e) {
            log.error("业务异常：", e);
        } finally {
            log.debug("业务结束");
        }
    }

    /**
     * 子类在重写此方法时，不要处理IO异常
     * @param socket
     * @param serverAddress
     * @param serverPort
     * @throws IOException
     */
    public abstract void run(DatagramSocket socket, InetAddress serverAddress, int serverPort) throws IOException;
}
